﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Business.PUBC;
using Mang24k99.Model.PUBC;
using Mang24k99.Model.COMM;

namespace Mang24k99.Web.PUBC
{
    public partial class BoardCyc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["no"] != null)
                {
                    string boardno = Request.QueryString["no"].ToString();
                    ddlBoardType.SelectedValue = boardno;
                }
                this.Panel1.Visible = true;
                this.Panel2.Visible = false;
                this.Panel3.Visible = true;
                BindBoardType();
                BindCycType();
                BindLimitType();
                BindGridView();
            }
        }
        /// <summary>
        /// 绑定调查表类型
        /// </summary>
        public void BindBoardType()
        {
            MKtPubcProduct[] productlist = (MKtPubcProduct[])BKtPubcProduct.SelectAll();
            this.ddlBoardType.DataSource = productlist;
            this.ddlBoardType.DataTextField = "BoardAlias";
            this.ddlBoardType.DataValueField = "BoardNO";
            this.ddlBoardType.DataBind();
        }
        /// <summary>
        /// 绑定周期类型
        /// </summary>
        public void BindCycType()
        {
            MKtCommCodeList[] codelist = (MKtCommCodeList[])BKtPubcCyc.SelectCycType();
            this.ddlCycType.DataSource = codelist;
            this.ddlCycType.DataTextField = "CODE_CNAME";
            this.ddlCycType.DataValueField="CODE_NO";
            this.ddlCycType.DataBind();
        }
        /// <summary>
        /// 绑定限制方式
        /// </summary>
        public void BindLimitType()
        {
            ddlLimitType.Items.Add(new ListItem("限制IP","1"));
            ddlLimitType.Items.Add(new ListItem("限制机器名", "2"));
            ddlLimitType.Items.Add(new ListItem("二者都限制", "3"));
            ddlLimitType.SelectedValue = "1";
        }

        public void BindGridView()
        {
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            MKtPubcCyc[] cyclist = (MKtPubcCyc[])BKtPubcCyc.SelectByBoardNO(boardno);
            grdList.DataSource = cyclist;
            grdList.DataBind();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            this.Panel3.Visible = false;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidatePage()==false)
            {
                return;
            }
            if (btnSave.Text=="新增")
            {
                if (NameValidate()==false)
                {
                    return;
                }
                if (AliasValidate()==false)
                {
                    return;
                }
                if (CyctypeValidate()==false)
                {
                    return;
                }
                MKtPubcCyc cyc = new MKtPubcCyc();
                cyc.CycNO = (int)MKtPubcCyc.GetMaxCycNO() + 1;
                //看板编号
                MKtPubcProduct product = BKtPubcProduct.Find(Convert.ToInt32(ddlBoardType.SelectedValue));
                //新增一个周期则该类型调查表周期数字段加一
                product.CycNum += 1;
                cyc.MKtPubcProduct = product;
                cyc.CycName = txtCycName.Text.Trim();
                cyc.CycAlias = txtCycAlias.Text.Trim();
                //周期类型
                MKtCommCodeList codelist = MKtCommCodeList.Select(Convert.ToInt32(ddlCycType.SelectedValue));
                cyc.MKtCommCodelist = codelist;
                cyc.LimitType = Convert.ToInt32(ddlLimitType.SelectedValue);
                cyc.FailureMsg = txtFailureMsg.Text.Trim();
                cyc.OvernumMsg = txtOverNumMsg.Text.Trim();
                cyc.SucessMsg = txtSuccessMsg.Text.Trim();
                cyc.TargetNum = 0;
                cyc.IsValidate = 1;
                cyc.LimitNum = Convert.ToInt32(txtLImitNum.Text.Trim());
                cyc.InputTime = DateTime.Now;
                try
                {
                    product.Update();
                    cyc.Create();
                    string strMsg = "新增成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {
                }
            }

            if (btnSave.Text=="更新")
            {
                int cycno = Convert.ToInt32(txtCycno.Text);
                MKtPubcCyc cyc = BKtPubcCyc.Find(cycno);
                if (txtCycName.Text.Trim()!=cyc.CycName)
                {
                    if (NameValidate()==false)
                    {
                        return;
                    }
                }
                if (txtCycAlias.Text.Trim()!=cyc.CycAlias)
                {
                    if (AliasValidate()==false)
                    {
                        return;
                    }
                }
                if (ddlCycType.SelectedValue!=cyc.MKtCommCodelist.CODE_NO.ToString())
                {
                    if (CyctypeValidate()==false)
                    {
                        return;
                    }
                }
                MKtCommCodeList codelist = MKtCommCodeList.Select(Convert.ToInt32(ddlCycType.SelectedValue));
                cyc.MKtCommCodelist = codelist;
                cyc.CycName = txtCycName.Text.Trim();
                cyc.CycAlias = txtCycAlias.Text.Trim();
                cyc.LimitType = Convert.ToInt32(ddlLimitType.SelectedValue);
                cyc.FailureMsg = txtFailureMsg.Text.Trim();
                cyc.OvernumMsg = txtOverNumMsg.Text.Trim();
                cyc.SucessMsg = txtSuccessMsg.Text.Trim();
                cyc.LimitNum = Convert.ToInt32(txtLImitNum.Text.Trim());
                cyc.ModifyTime = DateTime.Now;
                try
                {
                    cyc.Update();
                    string strMsg = "更新成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {     	
                }
            }
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (btnSave.Text=="新增")
            {
                ClearPage();
            }
            else
            {
                int cycno = Convert.ToInt32(txtCycno.Text);
                MKtPubcCyc cyc = BKtPubcCyc.Find(cycno);
                ddlBoardType.SelectedValue = cyc.MKtPubcProduct.BoardNO.ToString();
                txtCycName.Text = cyc.CycName;
                txtCycno.Text = cyc.CycNO.ToString();
                txtCycAlias.Text = cyc.CycAlias;
                ddlLimitType.SelectedValue = cyc.LimitType.ToString();
                txtLImitNum.Text = cyc.LimitNum.ToString();
                txtSuccessMsg.Text = cyc.SucessMsg;
                txtFailureMsg.Text = cyc.FailureMsg;
                txtOverNumMsg.Text = cyc.OvernumMsg;
            }
        }
        //清空页面
        public void ClearPage()
        {
            txtCycAlias.Text = "";
            txtCycName.Text = "";
            txtLImitNum.Text = "";
            txtCycno.Text = "";
            btnSave.Text = "新增";
            txtFailureMsg.Text = "上限50个汉字";
            txtOverNumMsg.Text = "上限50个汉字";
            txtSuccessMsg.Text = "上限50个汉字";
        }
        public bool ValidatePage()
        {
            if (txtCycName.Text.Trim()=="")
            {
                string strMsg = "周期名称不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (txtCycAlias.Text.Trim()=="")
            {
                string strMsg = "周期简称不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (txtLImitNum.Text.Trim()=="")
            {
                string strMsg = "限制次数不能为空！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (Common.PageValidate.IsNumber(txtLImitNum.Text.Trim())==false)
            {
                string strMsg = "限制次数输入格式不对，请输入正整数！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (Convert.ToInt32(txtLImitNum.Text.Trim())==0)
            {
                string strMsg = "限制次数不能为0！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            string strprompt = "上限50个汉字";
            if (txtSuccessMsg.Text.Trim()==strprompt)
            {
                string strMsg = "请输入投票成功信息！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (txtFailureMsg.Text.Trim()==strprompt)
            {
                string strMsg = "请输入投票失败信息！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            if (txtOverNumMsg.Text.Trim()==strprompt)
            {
                string strMsg = "请输入投票次数超限信息！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 验证周期名称是否重复
        /// </summary>
        /// <returns></returns>
        public bool NameValidate()
        {
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            string cycname = txtCycName.Text.Trim();
            MKtPubcCyc[] cyclist = (MKtPubcCyc[])BKtPubcCyc.NameValidate(boardno,cycname);
            if (cyclist.Length>0)
            {
                string strMsg = "已经有该周期名称，请更换名称！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 验证周期简称是否重复
        /// </summary>
        /// <returns></returns>
        public bool AliasValidate()
        {
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            string cycalias = txtCycAlias.Text.Trim();
            MKtPubcCyc[] cyclist = (MKtPubcCyc[])BKtPubcCyc.AliasValidate(boardno, cycalias);
            if (cyclist.Length > 0)
            {
                string strMsg = "已经有该周期简称，请更换名称！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 验证周期类型是否已经维护
        /// </summary>
        /// <returns></returns>
        public bool CyctypeValidate()
        {
            int boardno = Convert.ToInt32(ddlBoardType.SelectedValue);
            int cyctype = Convert.ToInt32(ddlCycType.SelectedValue);
            MKtPubcCyc[] cyclist = (MKtPubcCyc[])BKtPubcCyc.CycValidate(boardno, cyctype);
            if (cyclist.Length > 0)
            {
                string strMsg = "该周期类型已经维护！";
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            ClearPage();
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
        }

        protected void ddlBoardType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
            ClearPage();
            BindGridView();
        }

        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int cycno = Convert.ToInt32(e.CommandArgument.ToString());
            MKtPubcCyc cyc = BKtPubcCyc.Find(cycno);
            MKtPubcProduct product = cyc.MKtPubcProduct;
            //修改
            if (e.CommandName=="Modi")
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                this.Panel3.Visible = false;
                ddlBoardType.SelectedValue = cyc.MKtPubcProduct.BoardNO.ToString();
                ddlCycType.SelectedValue = cyc.MKtCommCodelist.CODE_NO.ToString();
                txtCycName.Text = cyc.CycName;
                txtCycno.Text = cyc.CycNO.ToString();
                txtCycAlias.Text = cyc.CycAlias;
                ddlLimitType.SelectedValue = cyc.LimitType.ToString();
                txtLImitNum.Text = cyc.LimitNum.ToString();
                txtSuccessMsg.Text = cyc.SucessMsg;
                txtFailureMsg.Text = cyc.FailureMsg;
                txtOverNumMsg.Text = cyc.OvernumMsg;
                btnSave.Text = "更新";
            }
            //删除
            if (e.CommandName=="Del")
            {
                try
                {
                    //删除一个周期则调查表类型表周期数字段数量减一
                    product.CycNum -= 1;
                    product.Update();
                    cyc.Delete();
                    string strMsg = "删除成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    ClearPage();
                    BindGridView();
                }
                catch (System.Exception ex)
                {

                }
            }
        }
    }
}
